package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.MesProduceLine;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.MesProduceLineDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-03-30 15:27:46
 */
public class MesProduceLineConverts {

    private static Logger LOG = LoggerFactory.getLogger(MesProduceLineConverts.class);

    public static List<MesProduceLineDTO> convert(List<MesProduceLine> mesProduceLineList) {
        if (CollectionUtils.isEmpty(mesProduceLineList)) {
            return Collections.EMPTY_LIST;
        }

        List<MesProduceLineDTO> mesProduceLineListResult = Lists.newArrayList();
        for (MesProduceLine mesProduceLine : mesProduceLineList) {
            mesProduceLineListResult.add(convert(mesProduceLine));
        }
        return mesProduceLineListResult;
    }

    public static MesProduceLineDTO convert(MesProduceLine mesProduceLine) {
        try {
            return BeanUtils.copy(mesProduceLine, MesProduceLineDTO.class);
        } catch (Exception e) {
            LOG.error("MesProduceLine到MesProduceLineDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesProduceLine到MesProduceLineDTO转换失败", e);
        }
    }

    public static MesProduceLine convert(MesProduceLineDTO mesProduceLineDTO) {
        try {
            return BeanUtils.copy(mesProduceLineDTO, MesProduceLine.class);
        } catch (Exception e) {
            LOG.error("MesProduceLineDTO到MesProduceLine转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesProduceLineDTO到MesProduceLine转换失败", e);
        }
    }
}